package com.food.restaurantpro.observers;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;

public class PreOrderDetailObserver implements Observer {

	/*private ProgressDialog _waitingDialog;*/
	private PreOrderDetailObserver _me = this;
	private boolean hadRegisterObserver = false;
	private PreOrderDetailObserverInterface mPreOrderDetailObserverInterface;
	
	private Activity mActivity;

	public PreOrderDetailObserver(PreOrderDetailObserverInterface preOrderDetailObserverInterface) {
		mPreOrderDetailObserverInterface = preOrderDetailObserverInterface;
	}

	/**
	 * register observer and addPreOrder
	 */
	public void registerObserver(Activity activity, String url) {
		mActivity = activity;
		if (ManagingNetwork.isNetworkAvailable()) {
			// show progress
			/*showProcessDialog();*/
			hadRegisterObserver = true;
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().addPreOrderDetail(url);

		} else {
			mPreOrderDetailObserverInterface.preOrderDetailCallback(null,
					RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.NETWORK_ERROR));
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			/*hideProcessDialog();*/
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		if (data == null) {
			/*hideProcessDialog();*/
			return;
		}
			
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case addPreOrderDetail:
			if (result.getErrorCode() != null) {
				// show error mesage
				mPreOrderDetailObserverInterface.preOrderDetailCallback(null, result
						.getErrorCode().getErrorMessage());
			} else {
				if (result.getData() != null) {
					//
					mPreOrderDetailObserverInterface.preOrderDetailCallback(
							(String) result.getData(), null);
				} else {
					mPreOrderDetailObserverInterface.preOrderDetailCallback(null,
							RestaurantApp.CURRENT_CONTEXT.getResources()
									.getString(R.string.SERVER_ERROR));
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve
		// thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong
		// "case method:"
		// khong dat code o ngoai nay
	}

	/*private void showProcessDialog() {
		if (_waitingDialog == null || !_waitingDialog.isShowing()) {
			_waitingDialog = new ProgressDialog(mActivity);
			_waitingDialog.setMessage(mActivity.getString(R.string.processing_pre_order));
			_waitingDialog.setCancelable(false);
			_waitingDialog.show();
		}
	}*/

	//Ham nay phai duoc goi trong onDestroy
	//Vi co truong hop dang request thi destroy activity
	//dialog se ko duoc dong neu ko dat ham nay trong onDestroy
	/*private void hideProcessDialog() {
		if (_waitingDialog != null && _waitingDialog.isShowing()) {
			_waitingDialog.dismiss();
		}
	}*/

}
